package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.wx.vo.TopicAndGoodsVo;
import com.cskaoyan.service.TopicService;
import com.cskaoyan.utils.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Description
 * 推广模块专题管理接口
 *
 * @author Zhangfei
 * @date Created on 2021/10/19
 */
@RestController
@RequestMapping("/admin/topic")
public class TopicController {
    @Autowired
    TopicService topicService;

    /**
     * 通过分页显示专题接口
     *
     * @param param
     * @return
     */
    @RequiresPermissions("admin:topic:list")
    @GetMapping("/list")
    public BaseRespVo list(String title, String subtitle, BaseParam param) {
        BasePageData data = topicService.list(title, subtitle, param);
        return BaseRespVo.ok(data);
    }

    /**
     * 通过专题id查询指定专题
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:topic:read")
    @GetMapping("/read")
    public BaseRespVo read(Integer id) {
        TopicAndGoodsVo topicAndGoodsVo = topicService.read(id);
        return BaseRespVo.ok(topicAndGoodsVo);
    }

    /**
     * 修改专题
     *
     * @param topic
     * @return
     */
    @RequiresPermissions("admin:topic:update")
    @PostMapping("/update")
    public BaseRespVo update(@RequestBody Topic topic) {
        Topic topicToReturn = topicService.update(topic);
        return BaseRespVo.ok(topicToReturn);
    }


    /**
     * 删除专题
     *
     * @param topic
     * @return
     */
    @RequiresPermissions("admin:topic:delete")
    @PostMapping("/delete")
    public BaseRespVo delete(@RequestBody Topic topic) {
        //执行删除，根据返回值响应相应的数据
        Integer count = topicService.delete(topic);
        if (count != 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除失败");
    }


    /**
     * 插入主题
     *
     * @param topic
     * @return
     */
    @RequiresPermissions("admin:topic:create")
    @PostMapping("/create")
    public BaseRespVo create(@RequestBody Topic topic) {
        Topic topicToReturn = topicService.create(topic);
        if (CommonUtil.isNull(topicToReturn)) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(topicToReturn);
    }

}
